package tx

import (
	"fmt"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/encrypt"
	"longmen/server/pkg/common/helper/http"
	string2 "longmen/server/pkg/common/helper/string"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/redis/cache"
	"strconv"
	"time"

	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
	live "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/live/v20180801"
)

const (
	connTimeoutMs  = 5000
	serveTimeoutMs = 10000
	defaultExpire  = 86400 * 180
	RePlayType     = 100

	txUrl = "https://console.tim.qq.com/v4"

	openimRouter     = "openim"
	sendmsgPath      = "sendmsg"
	batchsendmsgPath = "batchsendmsg"

	imOpenLoginSvcRouter = "im_open_login_svc"
	accountCheckPath     = "account_check"

	groupOpenHttpSvcRouter = "group_open_http_svc"
	createGroupPath        = "create_group"
	destroyGroupPath       = "destroy_group"
	sendGroupMsgPath       = "send_group_msg"
	getJoinedGroupListPath = "get_joined_group_list"
	deleteGroupMemberPath  = "delete_group_member"
	accountImportPath      = "account_import"
	forbidSendMsgPath      = "forbid_send_msg"
	accountDeletePath      = "account_delete"
)

// timelife = 0 时只发送在线用户
// ($touid,$desc,$ext=[],$timelife=604800)
func Sendmsg(touid, desc string, ext []string, timelife int) (*Response, error) {
	configPri := cache.GetConfigPri()
	sig, err := GenUserSig(util.S2I(configPri.ImSdkappid), configPri.ImSdksecret, configPri.TximAdmin, defaultExpire)
	if err != nil {
		return nil, err
	}
	random := string2.RandInt(0, 4294967295)
	apiPath := fmt.Sprintf("%s/%s/%s?sdkappid=%s&identifier=%s&usersig=%s&random=%d&contenttype=json",
		txUrl, openimRouter, sendmsgPath, configPri.ImSdkappid, configPri.TximAdmin, sig, random)

	extStr, _ := util.Struct2Json(ext)
	reqData := make(map[string]interface{})
	reqData["SyncOtherMachine"] = 2 // 消息不同步至发送方
	reqData["To_Account"] = touid
	reqData["MsgRandom"] = string2.RandInt(1000000, 9999999)
	reqData["MsgTimeStamp"] = time.Now().Unix()
	reqData["MsgLifeTime"] = timelife // 消息保存秒数
	reqData["MsgBody"] = []struct {
		MsgType    string
		MsgContent struct {
			Desc string
			Data string
		}
	}{
		{
			MsgType: "TIMCustomElem",
			MsgContent: struct {
				Desc string
				Data string
			}{
				Desc: desc,
				Data: extStr,
			},
		},
	}

	reqDataStr, _ := util.Struct2Json(&reqData)
	resp, err := http.HttpPost(apiPath, reqDataStr, connTimeoutMs, serveTimeoutMs, "application/json")
	if err != nil {
		return nil, err
	}

	result := &Response{}
	err = util.Json2Struct(string(resp), result)
	if err != nil {
		return nil, err
	}

	return result, nil
}

// 批量发送消息
// timelife = 0 时只发送在线用户
// ($touidArr,$elem,$timelife=604800)
func SendSystemMsg(touidArr []string, elem map[string]interface{}, timelife int) (*Response, error) {
	fName := "SendSystemMsg"
	if len(elem) <= 0 {
		return nil, nil
	}
	configPri := cache.GetConfigPri()
	sig, err := GenUserSig(util.S2I(configPri.ImSdkappid), configPri.ImSdksecret, configPri.TximAdmin, defaultExpire)
	if err != nil {
		return nil, err
	}
	random := string2.RandInt(0, 4294967295)
	apiPath := fmt.Sprintf("%s/%s/%s?sdkappid=%s&identifier=%s&usersig=%s&random=%d&contenttype=json",
		txUrl, openimRouter, batchsendmsgPath, configPri.ImSdkappid, configPri.TximAdmin, sig, random)

	reqData := make(map[string]interface{})
	reqData["SyncOtherMachine"] = 2
	reqData["To_Account"] = touidArr
	reqData["MsgRandom"] = string2.RandInt(1000000, 9999999)
	reqData["MsgTimeStamp"] = time.Now().Unix()
	reqData["MsgLifeTime"] = timelife
	reqData["MsgBody"] = []interface{}{elem}

	msgContent, ok := elem["MsgContent"].(map[string]interface{})
	if !ok {
		return nil, fmt.Errorf("%s elem MsgContent not a map[string]interface{}", util.Convert(fName))
	}
	reqData["OfflinePushInfo"] = struct {
		PushFlag int
		Title    string
		Desc     string
		Ext      string
	}{
		PushFlag: 0,
		Title:    msgContent["Title"].(string),
		Desc:     msgContent["Desc"].(string),
		Ext:      msgContent["Data"].(string),
	}

	reqDataStr, _ := util.Struct2Json(&reqData)
	resp, err := http.HttpPost(apiPath, reqDataStr, connTimeoutMs, serveTimeoutMs, "application/json")
	if err != nil {
		return nil, err
	}

	result := &Response{}
	err = util.Json2Struct(string(resp), result)
	if err != nil {
		return nil, err
	}

	return result, nil
}

// 批量发送广播 不会统计到未读消息数量
// timelife = 0 时只发送在线用户
// ($touidArr,$elem,$timelife=604800)
func SendBroadcast(touidArr []string, elem map[string]interface{}, timelife int) (*Response, error) {
	fName := "SendBroadcast"
	configPri := cache.GetConfigPri()
	sig, err := GenUserSig(util.S2I(configPri.ImSdkappid), configPri.ImSdksecret, configPri.TximBroadcast, defaultExpire)
	if err != nil {
		return nil, err
	}
	random := string2.RandInt(0, 4294967295)
	apiPath := fmt.Sprintf("%s/%s/%s?sdkappid=%s&identifier=%s&usersig=%s&random=%d&contenttype=json",
		txUrl, openimRouter, batchsendmsgPath, configPri.ImSdkappid, configPri.TximBroadcast, sig, random)

	reqData := make(map[string]interface{})
	reqData["SyncOtherMachine"] = 2
	reqData["To_Account"] = touidArr
	reqData["MsgRandom"] = string2.RandInt(1000000, 9999999)
	reqData["MsgTimeStamp"] = time.Now().Unix()
	reqData["MsgLifeTime"] = timelife
	reqData["MsgBody"] = []interface{}{elem}

	msgContent, ok := elem["MsgContent"].(map[string]interface{})
	if !ok {
		return nil, fmt.Errorf("%s elem MsgContent not a map[string]interface{}", util.Convert(fName))
	}
	reqData["OfflinePushInfo"] = struct {
		PushFlag int
		Title    string
		Desc     string
		Ext      string
	}{
		PushFlag: 0,
		Title:    msgContent["Title"].(string),
		Desc:     msgContent["Desc"].(string),
		Ext:      msgContent["Data"].(string),
	}

	reqDataStr, _ := util.Struct2Json(&reqData)
	resp, err := http.HttpPost(apiPath, reqDataStr, connTimeoutMs, serveTimeoutMs, "application/json")
	if err != nil {
		return nil, err
	}

	result := &Response{}
	err = util.Json2Struct(string(resp), result)
	if err != nil {
		return nil, err
	}

	return result, nil
}

func AcountCheck(userid string) (*Response, error) {
	configPri := cache.GetConfigPri()
	sig, err := GenUserSig(util.S2I(configPri.ImSdkappid), configPri.ImSdksecret, configPri.TximBroadcast, defaultExpire)
	if err != nil {
		return nil, err
	}
	random := string2.RandInt(0, 4294967295)
	apiPath := fmt.Sprintf("%s/%s/%s?sdkappid=%s&identifier=%s&usersig=%s&random=%d&contenttype=json",
		txUrl, imOpenLoginSvcRouter, accountCheckPath, configPri.ImSdkappid, configPri.TximBroadcast, sig, random)

	reqData := make(map[string]interface{})
	reqData["CheckItem"] = []struct {
		UserID string
	}{{UserID: userid}}

	reqDataStr, _ := util.Struct2Json(&reqData)
	resp, err := http.HttpPost(apiPath, reqDataStr, connTimeoutMs, serveTimeoutMs, "application/json")
	if err != nil {
		return nil, err
	}

	result := &Response{}
	err = util.Json2Struct(string(resp), result)
	if err != nil {
		return nil, err
	}

	return result, nil
}

// ($ownerid,$groupid,$type="ChatRoom")
func CreateChatRoom(ownerid, groupid, roomType string) (*Response, error) {
	configPri := cache.GetConfigPri()
	sig, err := GenUserSig(util.S2I(configPri.ImSdkappid), configPri.ImSdksecret, configPri.TximAdmin, defaultExpire)
	if err != nil {
		return nil, err
	}
	random := string2.RandInt(0, 4294967295)
	apiPath := fmt.Sprintf("%s/%s/%s?sdkappid=%s&identifier=%s&usersig=%s&random=%d&contenttype=json",
		txUrl, groupOpenHttpSvcRouter, createGroupPath, configPri.ImSdkappid, configPri.TximAdmin, sig, random)

	reqData := make(map[string]interface{})
	reqData["Owner_Account"] = ownerid
	reqData["Type"] = roomType
	reqData["Name"] = groupid
	reqData["GroupId"] = groupid

	reqDataStr, _ := util.Struct2Json(&reqData)
	resp, err := http.HttpPost(apiPath, reqDataStr, connTimeoutMs, serveTimeoutMs, "application/json")
	if err != nil {
		return nil, err
	}

	result := &Response{}
	err = util.Json2Struct(string(resp), result)
	if err != nil {
		return nil, err
	}

	return result, nil
}

func DestoryChatRoom(groupid string) (*Response, error) {
	configPri := cache.GetConfigPri()
	sig, err := GenUserSig(util.S2I(configPri.ImSdkappid), configPri.ImSdksecret, configPri.TximAdmin, defaultExpire)
	if err != nil {
		return nil, err
	}
	random := string2.RandInt(0, 4294967295)
	apiPath := fmt.Sprintf("%s/%s/%s?sdkappid=%s&identifier=%s&usersig=%s&random=%d&contenttype=json",
		txUrl, groupOpenHttpSvcRouter, destroyGroupPath, configPri.ImSdkappid, configPri.TximAdmin, sig, random)

	reqData := make(map[string]interface{})
	reqData["GroupId"] = groupid

	reqDataStr, _ := util.Struct2Json(&reqData)
	resp, err := http.HttpPost(apiPath, reqDataStr, connTimeoutMs, serveTimeoutMs, "application/json")
	if err != nil {
		return nil, err
	}

	result := &Response{}
	err = util.Json2Struct(string(resp), result)
	if err != nil {
		return nil, err
	}

	return result, nil
}

// 发送聊天室消息
// ($groupid,$elem)
func SendChatRoomMsg(groupid string, elem map[string]interface{}) (*Response, error) {
	configPri := cache.GetConfigPri()
	sig, err := GenUserSig(util.S2I(configPri.ImSdkappid), configPri.ImSdksecret, configPri.TximAdmin, defaultExpire)
	if err != nil {
		return nil, err
	}
	random := string2.RandInt(0, 4294967295)
	apiPath := fmt.Sprintf("%s/%s/%s?sdkappid=%s&identifier=%s&usersig=%s&random=%d&contenttype=json",
		txUrl, groupOpenHttpSvcRouter, sendGroupMsgPath, configPri.ImSdkappid, configPri.TximAdmin, sig, random)

	reqData := make(map[string]interface{})
	reqData["GroupId"] = groupid
	reqData["MsgRandom"] = string2.RandInt(1000000, 9999999)
	reqData["MsgBody"] = []interface{}{elem}

	reqDataStr, _ := util.Struct2Json(&reqData)
	resp, err := http.HttpPost(apiPath, reqDataStr, connTimeoutMs, serveTimeoutMs, "application/json")
	if err != nil {
		return nil, err
	}

	result := &Response{}
	err = util.Json2Struct(string(resp), result)
	if err != nil {
		return nil, err
	}

	return result, nil
}

// 获取用户加入的群组
// getMemberGroups($memberid,$groupType="")
func GetMemberGroups(memberid, groupType string) (*Response, error) {
	configPri := cache.GetConfigPri()
	sig, err := GenUserSig(util.S2I(configPri.ImSdkappid), configPri.ImSdksecret, configPri.TximAdmin, defaultExpire)
	if err != nil {
		return nil, err
	}
	random := string2.RandInt(0, 4294967295)
	apiPath := fmt.Sprintf("%s/%s/%s?sdkappid=%s&identifier=%s&usersig=%s&random=%d&contenttype=json",
		txUrl, groupOpenHttpSvcRouter, getJoinedGroupListPath, configPri.ImSdkappid, configPri.TximAdmin, sig, random)

	reqData := make(map[string]interface{})
	reqData["GroupType"] = groupType
	reqData["Member_Account"] = memberid

	reqDataStr, _ := util.Struct2Json(&reqData)
	resp, err := http.HttpPost(apiPath, reqDataStr, connTimeoutMs, serveTimeoutMs, "application/json")
	if err != nil {
		return nil, err
	}

	result := &Response{}
	err = util.Json2Struct(string(resp), result)
	if err != nil {
		return nil, err
	}

	return result, nil
}

// 移除群组成员
// ($groupid,$memberid=[])
func DeleteGroupMember(groupid string, memberid []string) (*Response, error) {
	configPri := cache.GetConfigPri()
	sig, err := GenUserSig(util.S2I(configPri.ImSdkappid), configPri.ImSdksecret, configPri.TximAdmin, defaultExpire)
	if err != nil {
		return nil, err
	}
	random := string2.RandInt(0, 4294967295)
	apiPath := fmt.Sprintf("%s/%s/%s?sdkappid=%s&identifier=%s&usersig=%s&random=%d&contenttype=json",
		txUrl, groupOpenHttpSvcRouter, deleteGroupMemberPath, configPri.ImSdkappid, configPri.TximAdmin, sig, random)

	reqData := make(map[string]interface{})
	reqData["GroupId"] = groupid
	reqData["MemberToDel_Account"] = memberid
	reqData["Silence"] = 1 // 是否静默删除（选填）

	reqDataStr, _ := util.Struct2Json(&reqData)
	resp, err := http.HttpPost(apiPath, reqDataStr, connTimeoutMs, serveTimeoutMs, "application/json")
	if err != nil {
		return nil, err
	}

	result := &Response{}
	err = util.Json2Struct(string(resp), result)
	if err != nil {
		return nil, err
	}

	return result, nil
}

// ($userid,$nick_name,$avatar=”)
func ImportIMUser(userid, nickName, avatar string) (*Response, error) {
	configPri := cache.GetConfigPri()
	sig, err := GenUserSig(util.S2I(configPri.ImSdkappid), configPri.ImSdksecret, configPri.TximAdmin, defaultExpire)
	if err != nil {
		return nil, err
	}
	random := string2.RandInt(0, 4294967295)
	apiPath := fmt.Sprintf("%s/%s/%s?sdkappid=%s&identifier=%s&usersig=%s&random=%d&contenttype=json",
		txUrl, groupOpenHttpSvcRouter, accountImportPath, configPri.ImSdkappid, configPri.TximAdmin, sig, random)

	reqData := make(map[string]interface{})
	reqData["Identifier"] = userid
	reqData["Nick"] = nickName
	reqData["FaceUrl"] = avatar

	reqDataStr, _ := util.Struct2Json(&reqData)
	resp, err := http.HttpPost(apiPath, reqDataStr, connTimeoutMs, serveTimeoutMs, "application/json")
	if err != nil {
		return nil, err
	}

	result := &Response{}
	err = util.Json2Struct(string(resp), result)
	if err != nil {
		return nil, err
	}

	return result, nil
}

// 禁言
func BanUser(groupid string, userids []string, shutUpTime int) (*Response, error) {
	configPri := cache.GetConfigPri()
	sig, _ := GenUserSig(util.S2I(configPri.ImSdkappid), configPri.ImSdksecret, configPri.TximAdmin, defaultExpire)
	random := string2.RandInt(0, 4294967295)
	apiPath := fmt.Sprintf("%s/%s/%s?sdkappid=%s&identifier=%s&usersig=%s&random=%d&contenttype=json",
		txUrl, groupOpenHttpSvcRouter, forbidSendMsgPath, configPri.ImSdkappid, configPri.TximAdmin, sig, random)

	reqData := make(map[string]interface{})
	reqData["GroupId"] = groupid
	reqData["Members_Account"] = userids
	reqData["ShutUpTime"] = shutUpTime //0 取消禁言

	reqDataStr, _ := util.Struct2Json(&reqData)
	resp, err := http.HttpPost(apiPath, reqDataStr, connTimeoutMs, serveTimeoutMs, "application/json")
	if err != nil {
		return nil, err
	}

	result := &Response{}
	err = util.Json2Struct(string(resp), result)
	if err != nil {
		return nil, err
	}

	return result, nil
}

// ($id,$name,$avatar=”)
func ImportAccount(id, name, avatar string) (*Response, error) {
	configPri := cache.GetConfigPri()
	sig, _ := GenUserSig(util.S2I(configPri.ImSdkappid), configPri.ImSdksecret, configPri.TximAdmin, defaultExpire)
	random := string2.RandInt(0, 4294967295)
	apiPath := fmt.Sprintf("%s/%s/%s?sdkappid=%s&identifier=%s&usersig=%s&random=%d&contenttype=json",
		txUrl, groupOpenHttpSvcRouter, accountImportPath, configPri.ImSdkappid, configPri.TximAdmin, sig, random)

	reqData := make(map[string]interface{})
	reqData["Identifier"] = id
	reqData["Nick"] = name
	reqData["FaceUrl"] = avatar

	reqDataStr, _ := util.Struct2Json(&reqData)
	resp, err := http.HttpPost(apiPath, reqDataStr, connTimeoutMs, serveTimeoutMs, "application/json")
	if err != nil {
		return nil, err
	}

	result := &Response{}
	err = util.Json2Struct(string(resp), result)
	if err != nil {
		return nil, err
	}

	return result, nil
}

func DeleteAccount(id string) (*Response, error) {
	configPri := cache.GetConfigPri()
	sig, _ := GenUserSig(util.S2I(configPri.ImSdkappid), configPri.ImSdksecret, configPri.TximAdmin, defaultExpire)
	random := string2.RandInt(0, 4294967295)
	apiPath := fmt.Sprintf("%s/%s/%s?sdkappid=%s&identifier=%s&usersig=%s&random=%d&contenttype=json",
		txUrl, groupOpenHttpSvcRouter, accountDeletePath, configPri.ImSdkappid, configPri.TximAdmin, sig, random)

	reqData := make(map[string]interface{})
	reqData["DeleteItem"] = []struct {
		UserID string
	}{{UserID: id}}

	reqDataStr, _ := util.Struct2Json(&reqData)
	resp, err := http.HttpPost(apiPath, reqDataStr, connTimeoutMs, serveTimeoutMs, "application/json")
	if err != nil {
		return nil, err
	}

	result := &Response{}
	err = util.Json2Struct(string(resp), result)
	if err != nil {
		return nil, err
	}

	return result, nil
}

func BuildCustomElem(action string, data interface{}, title string, desc string) map[string]interface{} {
	elem := make(map[string]interface{})
	elem["MsgType"] = "TIMCustomElem"

	eledata := make(map[string]interface{})
	eledata["Action"] = action
	eledata["Data"] = data
	eledataStr, _ := util.Struct2Json(&eledata)
	msgContent := map[string]interface{}{"Desc": desc, "Title": title, "Data": eledataStr}
	elem["MsgContent"] = msgContent
	return elem
}

func CheckPushActive(stream string) (bool, error) {
	configPri := cache.GetConfigPri()
	cred := common.NewCredential(configPri.QcloudLiveSecretid, configPri.QcloudLiveSecretkey)
	httpProfile := profile.NewHttpProfile()
	httpProfile.Endpoint = "live.tencentcloudapi.com"
	clientProfile := profile.NewClientProfile()
	clientProfile.HttpProfile = httpProfile
	client, err := live.NewClient(cred, "", clientProfile)
	if err != nil {
		return false, fmt.Errorf("%s%s", util.Convert2(), err.Error())
	}
	req := live.NewDescribeLiveStreamStateRequest()
	params := make(map[string]interface{})
	params["AppName"] = "models_live"
	params["DomainName"] = configPri.PushDomain
	params["StreamName"] = stream
	paramsStr, _ := util.Struct2Json(params)
	err = req.FromJsonString(paramsStr)
	if err != nil {
		return false, fmt.Errorf("%s%s", util.Convert2(), err.Error())
	}
	resp, err := client.DescribeLiveStreamState(req)
	if err != nil {
		return false, fmt.Errorf("%s%s", util.Convert2(), err.Error())
	}
	return *resp.Response.StreamState == "active", nil
}

func ForbidLiveStream(stream string) (string, error) {
	return "", nil
	configPri := cache.GetConfigPri()
	credential := common.NewCredential(
		configPri.QcloudLiveSecretid,
		configPri.QcloudLiveSecretkey,
	)
	cpf := profile.NewClientProfile()
	//cpf.HttpProfile.Endpoint = "models_live.tencentcloudapi.com"
	client, _ := live.NewClient(credential, "", cpf)
	request := live.NewForbidLiveStreamRequest()
	request.AppName = common.StringPtr("models_live")
	request.DomainName = common.StringPtr(configPri.PushDomain)
	request.StreamName = common.StringPtr(stream)
	response, err := client.ForbidLiveStream(request)
	//if _, ok := err.(*errors.TencentCloudSDKError); ok {
	//	global.Log.Error("An API error has returned: %s", err)
	//	return "", err
	//}
	if err != nil {
		global.Log.Error("An API error has returned: %s", err)
		return "", err
	}
	return *response.Response.RequestId, nil
}

const (
	HostFLV  = "flv"
	HostHLS  = "hls"
	HostRTMP = "rtmp"
	HostUDP  = "udp"
)

/**
 *  @desc 腾讯云推拉流地址
 *  @param string $host 协议，如:http、rtmp
 *  @param string $stream 流名,如有则包含 .flv、.m3u8
 *  @param int $type 类型，0表示播流，1表示推流
 */
func CreateLiveUrl(host, stream string, liveType int) []string {
	var pullExpTime int64
	pullExpTime = 3600 * 2 //秒
	if host == HostHLS {
		pullExpTime = 3600 * 3
	}
	configPri := cache.GetConfigPri()
	nowTime := time.Now().Unix()
	pullTime := strconv.FormatInt(nowTime+pullExpTime, 16)
	pushTime := strconv.FormatInt(nowTime+1800, 16)
	pushTxSecret := encrypt.Md5(configPri.PushKey + stream + pushTime)
	safePushUrl := "?txSecret=" + pushTxSecret + "&txTime=" + pushTime
	pullTxSecret := encrypt.Md5(configPri.PullKey + stream + pullTime)
	safePullUrl := "?txSecret=" + pullTxSecret + "&txTime=" + pullTime
	if liveType == 1 {
		url := fmt.Sprintf("rtmp://%s/live/", configPri.PushDomain)
		webRtcurl := fmt.Sprintf("webrtc://%s/live/", configPri.PushDomain)
		anchorSecret := stream + safePushUrl
		return []string{url, webRtcurl, anchorSecret}
	} else {
		url := ""
		if host == HostRTMP {
			url = fmt.Sprintf("rtmp://%s/live/", configPri.PullDomain) + stream + safePullUrl
		} else if host == HostFLV {
			url = fmt.Sprintf("https://%s/live/", configPri.PullDomain) + stream + ".flv" + safePullUrl
		} else if host == HostHLS {
			url = fmt.Sprintf("https://%s/live/", configPri.PullDomain) + stream + ".m3u8" + safePullUrl
		} else if host == HostUDP {
			url = fmt.Sprintf("webrtc://%s/live/", configPri.PullDomain) + stream + safePullUrl
		}
		//global.Log.Debugf("推流域名:%s,url:%s", configPri.PullDomain, url)
		return []string{url}
	}
}

// 专给卫星源用
func CreateSatelliteLiveUrl(host, stream string, liveType int) []string {
	configPri := cache.GetConfigPri()
	var pullExpTime int64
	pullExpTime = 30 //秒
	if len(global.Config.GetString("nami.pull_satellite_url")) > 0 {
		configPri.PullDomain = global.Config.GetString("nami.pull_satellite_url")
	}
	if len(global.Config.GetString("nami.pull_satellite_key")) > 0 {
		configPri.PullKey = global.Config.GetString("nami.pull_satellite_key")
	}
	if host == HostHLS {
		pullExpTime = 3600 * 3
	}
	nowTime := time.Now().Unix()
	pullTime := strconv.FormatInt(nowTime+pullExpTime, 16)
	pushTime := strconv.FormatInt(nowTime+1800, 16)
	pushTxSecret := encrypt.Md5(configPri.PushKey + stream + pushTime)
	safePushUrl := "?txSecret=" + pushTxSecret + "&txTime=" + pushTime
	pullTxSecret := encrypt.Md5(configPri.PullKey + stream + pullTime)
	safePullUrl := "?txSecret=" + pullTxSecret + "&txTime=" + pullTime

	if liveType == 1 {
		url := fmt.Sprintf("rtmp://%s/live/", configPri.PushDomain)
		webRtcurl := fmt.Sprintf("webrtc://%s/live/", configPri.PushDomain)
		anchorSecret := stream + safePushUrl
		return []string{url, webRtcurl, anchorSecret}
	} else {
		url := ""
		if host == HostRTMP {
			url = fmt.Sprintf("rtmp://%s/live/", configPri.PullDomain) + stream + safePullUrl
		} else if host == HostFLV {
			url = fmt.Sprintf("https://%s/live/", configPri.PullDomain) + stream + ".flv" + safePullUrl
		} else if host == HostHLS {
			url = fmt.Sprintf("https://%s/live/", configPri.PullDomain) + stream + ".m3u8" + safePullUrl
		} else if host == HostUDP {
			url = fmt.Sprintf("webrtc://%s/live/", configPri.PullDomain) + stream + safePullUrl
		}
		//global.Log.Debugf("推流域名:%s,url:%s", configPri.PullDomain, url)
		return []string{url}
	}
}
